<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class AmazonAPI extends CI_Controller {

	public function index()
	{
		$this->load->helper('url');
		$this->load->view('v_amazonAPI');
	}
	
	//Return status, code, bill array
	public function getBills()
	{
		$this->load->model('m_amazonAPI');
		
		//Expect to be sent userid, vendorname, 
		$userid = $this->input->post('userid', TRUE);
		$vendorid = $this->input->post('vendorid', TRUE);
		
		$messageresponse;
		
		//Verify user exists; verify userID		
		$error_code = $this->m_amazonAPI->validate_user($userid);
		if($error_code == 0)
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
			
			//Get bill(s) of the specified userID of this vendor
			$recordset = $this->m_amazonAPI->get_bills($userid);
			$bill = $recordset->result();
			$messageresponse->bills = $bill;
		}
		else if ($error_code = -1)
		{
			$messageresponse->status = "Error: Invalid userID";
			$messageresponse->code = -1;
		}		
		
		//Sending back 
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}

	public function linkToVendor()
	{
		//Expect to be sent a userid and vendor name
		$userid = $this->input->post('userid', TRUE);
		$vendorname = $this->input->post('vendorname', TRUE);
		
		$messageresponse;
		
		//Validate userid
		if($userid == 1 || $userid == 2 || $userid == 3 || $userid == 4 || $userid == 5)
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		} 
		else {
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
			//$messageresponse->description = "Not a valid userID";
		}
		
		//Validate vendor name
		if($vendorname == "Verizon" || $vendorname == "Best Buy" || $vendorname == "Amazon" || $vendorname == "CSULB" || $vendorname == "Time Warner Cable")
		{
			//userID already caused an error, return an error
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Not a valid userID";
			}
			else //No userID error and no vendorname error
			{
				$messageresponse->description = "Successfully linked to vendor";
			}
		}
		else 
		{
			//Invalid userid AND vendorname
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Invalid userid and vendor name";
			}
			else //Valid userid, but invalid vendorname
			{
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
				$messageresponse->description = "Error - Invalid vendor name";
			}
		}
		
		//Sending back status, code, description
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}
	
	public function unlinkVendor()
	{
		//Expect to be sent a userid and vendor name
		$userid = $this->input->post('userid', TRUE);
		$vendorname = $this->input->post('vendorname', TRUE);
		
		$messageresponse;
		
		//Validate userid
		if($userid == 1 || $userid == 2 || $userid == 3 || $userid == 4 || $userid == 5)
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		} 
		else {
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
			//$messageresponse->description = "Not a valid userID";
		}
		
		//Validate vendor name
		if($vendorname == "CSULB" || $vendorname == "Best Buy" || $vendorname == "Amazon" || $vendorname == "CSULB" || $vendorname == "Time Warner Cable")
		{
			//userID already caused an error, return an error
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Not a valid userID";
			}
			else //No userID error and no vendorname error
			{
				$messageresponse->description = "Successfully unlinked vendor";
			}
		}
		else 
		{
			//Invalid userid AND vendorname
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Invalid userid and vendor name";
			}
			else //Valid userid, but invalid vendorname
			{
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
				$messageresponse->description = "Error - Invalid vendor name";
			}
		}
		
		//Sending back status, code, description
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}
	
	//Billing Service sends message to Vendor Service that a bill has been paid
	public function billPaid() 
	{
		//Expect to be sent a billstate => 'paid'
		$billstate = $this->input->post('billstate', TRUE);
		$invoiceID = $this->input->post('invoiceID', TRUE);
		$amount = $this->input->post('amount', TRUE);
		
		if($billstate == "PAID")
		{
			$this->load->model('m_amazonAPI');
			
			$recordset = $this->m_amazonAPI->paybill($invoiceID);
			
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		}
		else { //Error, state variable that was sent had an error
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
		}
		
		//Sending back status, code
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}

}